/*******************************************************************************
* Copyright (c) 2010, 2018 西安秦晔信息科技有限公司
* Licensed under the Apache License, Version 2.0 (the "License");
*    you may not use this file except in compliance with the License.
*    You may obtain a copy of the License at
*
*       http://www.apache.org/licenses/LICENSE-2.0
*
*    Unless required by applicable law or agreed to in writing, software
*    distributed under the License is distributed on an "AS IS" BASIS,
*    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*    See the License for the specific language governing permissions and
*    limitations under the License.
*******************************************************************************/
package com.qinyeit.webapp.controller.management.branchstores;

import com.alibaba.fastjson.JSONObject;
import com.qinyeit.serviceapp.entity.District;
import com.qinyeit.serviceapp.entity.Stock;
import com.qinyeit.serviceapp.service.branchs.BranchDataServiceImpl;
import com.qinyeit.serviceapp.service.organization.DistrictService;
import com.qinyeit.webapp.controller.management.BaseManagementController;
import com.qinyeit.webapp.utils.CurrentAccountUtils;
import com.qinyetech.springstage.core.entity.search.Searchable;
import com.qinyetech.springstage.core.lang.QyMap;
import com.qinyetech.springstage.core.utils.JSONUtils;
import com.qinyetech.springstage.core.web.annotation.PageableDefaults;
import com.qinyetech.springstage.core.web.annotation.PermissionInfo;
import com.qinyetech.springstage.core.web.dto.ResponseResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * ClassName: StocksController <br/>
 * Function: stock [库存] <br/>
 * date: 2018-11-10 14:56:59 <br/>
 *
 * @author liuwenjin
 * @version  1.0
 * @since JDK 1.8
 */
@RestController
@RequestMapping("/management/branchstores/branchStocks")
@PermissionInfo(parentName ="基础信息管理" ,parentOrder = 10,order = 10,code = "management_branchStock:read",name ="库存",deleteAble = false)
public class BranchStocksController extends BaseManagementController{

    @Autowired
    private DistrictService districtService;
    @Autowired
    private BranchDataServiceImpl branchDataService;

    /**
     * 库存查询
     * @param searchable
     * @param categoryIds 类别
     * @param warehouseId 仓库
     * @param flag flag 高于最高库存 max / 低于最低库存 min
     * @param keyword  关键字 （按物料名称/编码/简码/拼音码）
     * @param merchantId
     * @return
     */
    @RequiresPermissions("management_branchStock:show")
    @PermissionInfo(code = "management_branchStock:show",name ="查看",deleteAble = false)
    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST})
    @PageableDefaults(sort = "id=desc")//默认按照id降序
    public ResponseResult<JSONObject> index(Searchable searchable, String categoryIds, Long warehouseId, String flag, String keyword,Long merchantId){
//        Page<Stock> stockPage = stockService.findAll(searchable);
        QyMap map = QyMap.NEW();
        if (merchantId!=null){
            List<QyMap> mapList = branchDataService.queryStock(categoryIds, warehouseId, flag, keyword, searchable.getPage(),merchantId);
            map = QyMap.map("stockList",mapList);
        }
        initFormDate(map);
        return ok("查询成功", map);
    }


    @GetMapping("/getBranchWarehouses")
    public Object getBranchWarehouses(Long merchantId,Long branchId){
        QyMap map = QyMap.NEW();
        List<Map<String,Object>> warehouses = branchDataService.getBranchWarehouses(merchantId);
        map.setv("warehouses",warehouses);
        //物品类别
        List<Map<String,Object>> categorys = branchDataService.getMaterialCategorys(branchId.toString());
        map.setv("categorys",categorys);

        return ok("查询成功", map);
    }


    /**
     * 初始化表单数据
     * @param qyMap
     */
    private void initFormDate(QyMap qyMap){
        //直配门店
        List<District> list=districtService.getBuildTree(CurrentAccountUtils.getAuthenticatedMerchantGroupID(),true);
        qyMap.setv("districts",list);

    }

}